﻿package utils {
	import flash.geom.Point;
	
//	import spark.layouts.RowAlign;
//	import spark.primitives.Rect;

	public class Isometric {
		private static const  _sin30:Number=Math.sin(Math.PI/6);
		private static const  _cos30:Number=Math.cos(Math.PI/6);
		private static const  _sin45:Number=Math.sin(Math.PI/4);
		private static const  _cos45:Number=Math.cos(Math.PI/4);

		public static function mapToScreen(xpp:Number, ypp:Number, zpp:Number):Point{
			var yp:Number = ypp;
			var xp:Number = xpp*_cos45+zpp*_sin45;
			var zp:Number = zpp*_cos45-xpp*_sin45;
			var x:Number = xp;
			var y:Number = yp*_cos30-zp*_sin30;

			return new Point(x, y);
		}

		public static function mapToIsoWorld(screenX:Number, screenY:Number):Point {
			var z:Number = (screenX/_cos45-screenY/(_sin45*_sin30))*(1/(_cos45/_sin45+_sin45/_cos45));
			var x:Number = (1/_cos45)*(screenX-z*_sin45);
			return new Point(x, z);
		}
		
		public static function getRectElement(row:int,col:int,width:int,height:int,dis:int=1):Vector.<Point>{
			var aps:Vector.<Point>=new Vector.<Point>();
			var i:int = row-dis;
			var j:int = col-dis;
			
			var ie:int = row+width+dis;
			var je:int = col+height+dis;
			
			for(var si:int=i;si<ie;si++){
			     for(var sj:int=j;sj<je;sj++){
				    aps.push(new Point(si,sj));
				 }
			}
			return aps;
		}
		
		/**
		 * 获取相邻，对角不能取。 
		 * @param row
		 * @param col
		 * @param width
		 * @param height
		 * @param dis
		 * @return 
		 * 
		 */
		public static function getNeighbor(row:int,col:int,width:int,height:int,dis:int=1):Vector.<Point>{
			//trace(row,col,width,height);
			var aps:Vector.<Point>=new Vector.<Point>();
			var i : int=0;
			for(i =row;i<row+width;i++){
				aps.push(new Point(i,col-dis))
			}

			for( i=col;i<col+height;i++){
				aps.push(new Point(row-dis,i))
			}
			////
			for( i =row;i<row+width;i++){
				aps.push(new Point(i,col+height+dis-1))
			}

			for( i=col;i<col+height;i++){
				aps.push(new Point(row+width+dis-1,i))
			}

			aps.push(new Point(row-dis,col-dis))
			aps.push(new Point(row+width+dis-1,col-dis))
			aps.push(new Point(row-dis,col+height+dis-1))
			aps.push(new Point(row+width+dis-1,col+height+dis-1))

			return aps;
		}

	}
}